//
//  MyTouziViewController.m
//  TenThousand
//
//  Created by 我的 on 16/7/18.
//  Copyright © 2016年 baixue. All rights reserved.
//

#import "MyTouziViewController.h"
#import "MyInvestViewCell.h"
#import "RedemptionViewController.h"//提前赎回
#import "TheredemptionViewController.h"//赎回中
#import "MyTouziModel.h"

@interface MyTouziViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    BOOL _isLastPage;
    
}

@property (nonatomic, strong) UITableView *touziTableView;
@property (nonatomic, strong) NSMutableArray *touziArr;
@end

static NSInteger currentPage = 1;

@implementation MyTouziViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.touziTableView];
}

//存放解析的数据
- (NSMutableArray *)touziArr{
    if (!_touziArr) {
        _touziArr = [NSMutableArray array];
    }
    return _touziArr;
}


//懒加载tableView
- (UITableView *)touziTableView{
    if (!_touziTableView) {
        _touziTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Height-64-40) style:UITableViewStyleGrouped];
        _touziTableView.delegate = self;
        _touziTableView.dataSource = self;
        _touziTableView.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1.00];
        _touziTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _touziTableView.showsVerticalScrollIndicator = NO;

    }
        return _touziTableView;
}

//行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}


//组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.touziArr.count;
}


//cell复用
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MyInvestViewCell *investCell = [tableView dequeueReusableCellWithIdentifier:@"MYINVESTVIEWCELL"];
    if (investCell == nil) {
        investCell = [[[NSBundle mainBundle] loadNibNamed:@"MyInvestViewCell" owner:nil options:nil] lastObject];
    }
    
     investCell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    //cell添加阴影
    investCell.layer.shadowOffset =CGSizeMake(0, 1);
    investCell.layer.shadowColor = [UIColor grayColor].CGColor;
    investCell.layer.shadowRadius = 1;
    investCell.layer.shadowOpacity = .5f;
    
    
    if (self.touziArr.count) {
        MyTouziModel *model = self.touziArr[indexPath.section];
        if (self.index == 3 || self.index == 4) {
            [investCell setDataAndModel:model];
        }else
            
            [investCell setDataWithModel:model];
    }
    
    return investCell;
}

//行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

//header
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
        return 1;
    }
    return 8;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}
//tableView点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    if (self.index == 1) {
        //提前赎回
        RedemptionViewController *redempView = [[RedemptionViewController alloc]init];
        
        MyTouziModel *model = self.touziArr[indexPath.section];
        redempView.model = model;
        redempView.num = 1;
        [self setHidesBottomBarWhenPushed:YES];
        [self.navigationController pushViewController:redempView animated:YES];
        
    }else if(self.index == 3){
        //赎回中
        TheredemptionViewController *theRedView = [[TheredemptionViewController alloc]init];
        MyTouziModel *model = _touziArr[indexPath.section];
        theRedView.model = model;
        theRedView.num = 3;
        [self setHidesBottomBarWhenPushed:YES];
        [self.navigationController pushViewController:theRedView animated:YES];
    }else if(self.index == 2){
        //已结清
        RedemptionViewController *redempView = [[RedemptionViewController alloc]init];
        
        MyTouziModel *model = self.touziArr[indexPath.section];
        redempView.model = model;
        redempView.num = 2;
        [self setHidesBottomBarWhenPushed:YES];
        [self.navigationController pushViewController:redempView animated:YES];
    }else if (self.index == 4){
        //提前赎回
        RedemptionViewController *redempView = [[RedemptionViewController alloc]init];
        
        MyTouziModel *model = self.touziArr[indexPath.section];
        redempView.model = model;
        redempView.num = 4;
        [self setHidesBottomBarWhenPushed:YES];
        [self.navigationController pushViewController:redempView animated:YES];
    }
}



- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    NSString *method;
    if (self.index == 4) {
        //已赎回
        method = @"Redemp.GetretiredList";
    }else if (self.index == 3){
        method = @"Redemp.GetredemptionList";//赎回中
    }else if (self.index == 2){
        method = @"invest.GetinvestListFinance";//已结清列表
    }else if(self.index == 1){
        method = @"invest.GetinvestListunderway";//投资中列表
    }else
    
    method = @"invest.GetinvestListALL";//全部投资列表
    
    
    [self getinvestList:method WithPage:1];
    
    [self refreshPageWithMethod:method];

    
}

//调用接口
- (void)getinvestList:(NSString *)method WithPage:(NSInteger)page{
    NSString *index = [NSString stringWithFormat:@"%ld",page];
    NSString *loginId = [[NSUserDefaults standardUserDefaults] objectForKey:@"LoginId"];
    NSDictionary *dict = @{
                           @"PageIndex":index,
                           @"LoginId":loginId
                           };
    
    [NetWorkManager requestWithType:HttpRequestTypePost withMethodName:method withParaments:dict withSuccessBlock:^(NSDictionary *object) {
        
        
        NSLog(@"=====%@",dict);
        NSLog(@"=====%@", object);
        
        
        
        if (page == 1) {

            
            [self.touziArr removeAllObjects];
            currentPage = 1;
            _isLastPage = NO;
            
            NSArray *arr = [object[@"Data"] firstObject][@"ItemList"];
            for (NSDictionary *dic in arr) {
                MyTouziModel *model = [MyTouziModel mj_objectWithKeyValues:dic];
                [_touziArr addObject:model];
            }

            
        }else
        {
            
            
            NSArray *arr = [object[@"Data"] firstObject][@"ItemList"];
           

            if (arr && [arr count]) {
                _isLastPage = NO;
                for (NSDictionary *dic in arr) {
                    MyTouziModel *model = [MyTouziModel mj_objectWithKeyValues:dic];
                    [_touziArr addObject:model];
                }
                
            }else
            {
                _isLastPage = YES;
            }
            
        }

        [self.touziTableView reloadData];
    } withFailureBlock:^(NSError *error) {
        NSLog(@"%@", error);
        [ToastView showErrorWithStatus:@"网络请求失败"];
    } progress:^(float progress) {
        
    }];
}



//MJ刷新
-(void)refreshPageWithMethod:(NSString *)method
{
    
    //下拉刷新
    self.touziTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^
                                      {
                                          
                                          [self getinvestList:method WithPage:1];
                                          
                                          [self.touziTableView.mj_header endRefreshing];
                                      }];
    //上拉加载
    self.touziTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        
        
        if (_isLastPage) {
            [self.touziTableView.mj_footer endRefreshing];
            return;
        }
        
        [self getinvestList:method WithPage:++currentPage];
        
        [self.touziTableView.mj_footer endRefreshing];
    }];
    
    
}
@end
